package org.jsping.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.jsping.dao.ISingerDao;
import org.jsping.entity.Singer;

public class SingerDaoImpl extends BaseDao implements ISingerDao {

	//添加一个歌手
	//添加成功返回true
	@Override
	public boolean addSinger(Singer singer) throws SQLException {
		String sql = "INSERT SingerList(Sname,Register,Picture) VALUES(?,?,?)";
		int result = this.executeUpdate(sql, singer.getSname(),singer.getRegister(),singer.getPicture());
		if(result>0) {
			return true;
		}
		return false;
	}
	/**
	 * 查找歌手,有歌手存在返回true
	 * @throws SQLException 
	 */
	@Override
	public Singer selectSinger(String name) throws SQLException {
		String sql = "SELECT * FROM SingerList WHERE Sname = ?";
		ResultSet result = this.executeQuery(sql, name);
		try {
			if(result.next()) {
				Singer singer = new Singer();
				singer.setSid(result.getInt("Sid"));;
				singer.setSname(result.getString("Sname"));;
				singer.setRegister(result.getString("Register"));;
				singer.setPicture(result.getString("Picture"));;
				return singer;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			this.releaseAll();
		}
		return null;
	}
	//获取歌手id
	@Override
	public int selectSingerID(String name) throws SQLException {
		String sql = "SELECT Sid FROM SingerList WHERE Sname = ?";
		ResultSet rs = this.executeQuery(sql, name);
		if(rs.next()) {
			return rs.getInt("Sid");
		}
		return 0;
	}
}
